package edu.ccut.saturn.studio.dialog.iterator;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.ElementListSelectionDialog;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnSWTUtils;
import edu.ccut.saturn.studio.dialog.SelectionCandidate;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnStartIterationNode;

public class SaturnStartIteratorDialog extends TitleAreaDialog {

	private Text idText;
	private Combo endIteratorIdCombo;
	private Text itemTxt;
	private Text nameText, valueText;
	private Text descriptionText;
	
	private Label nameLabel, valueLabel, descriptionLabel, browser;
	
	private SaturnStartIterationNode saturnStartIterationNode;
	
	public SaturnStartIteratorDialog(Shell parentShell,SaturnStartIterationNode saturnViewerNode) {
		super(parentShell);
		this.saturnStartIterationNode = saturnViewerNode;
	}
	
	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		setMessage(SelectionCandidate.SETTING_ATTRIBUTE + saturnStartIterationNode.getName()); 
		return contents;
	}
	
	@Override
	protected Control createDialogArea(Composite parent) {
		setMessage(SelectionCandidate.SETTING_ATTRIBUTE + saturnStartIterationNode.getName().trim());
		setTitleImage(Activator.getImage(Images.SATURN_ICONS_MAXNAME)); //$NON-NLS-1$
		
		Composite area = (Composite) super.createDialogArea(parent);
		Composite container = new Composite(area, SWT.NONE);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		container.setLayout(gridLayout);
		container.setLayoutData(new GridData(GridData.FILL_BOTH));

		final Label idLabel_1 = new Label(container, SWT.NONE);
		idLabel_1.setText(Messages.getString("SaturnStartIteratorDialog.ID")); //$NON-NLS-1$

		idText = new Text(container, SWT.READ_ONLY | SWT.BORDER);
		final GridData gd_idText = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
		gd_idText.horizontalIndent = 5;
		gd_idText.verticalIndent = 5;
		gd_idText.verticalIndent = 5;
		idText.setLayoutData(gd_idText);
		idText.setText(saturnStartIterationNode.getId().trim());
		idText.setSelection(saturnStartIterationNode.getId().trim().length());
		
		nameLabel = new Label(container, SWT.NONE);
		final GridData gd_nameLabel = new GridData();
		gd_nameLabel.verticalIndent = 15;
		nameLabel.setLayoutData(gd_nameLabel);
		nameLabel.setText(Messages.getString("SaturnStartIteratorDialog.name"));   //$NON-NLS-1$
		
		nameText = new Text(container, SWT.BORDER);
		nameText.setTextLimit(50);
		final GridData gd_textName = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
		gd_textName.horizontalIndent = 5;
		gd_textName.verticalIndent = 5;
		nameText.setLayoutData(gd_textName);
		nameText.setText(saturnStartIterationNode.getName().trim());
		nameText.setSelection(saturnStartIterationNode.getName().trim().length());

		valueLabel = new Label(container, SWT.NONE);
		final GridData gd_locationLabel = new GridData();
		gd_locationLabel.verticalIndent = 15;
		valueLabel.setLayoutData(gd_locationLabel);
		valueLabel.setText(Messages.getString("SaturnStartIteratorDialog.value"));   //$NON-NLS-1$
		
		valueText = new Text(container, SWT.BORDER);
		valueText.setTextLimit(100);
		final GridData gd_textLocation = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_textLocation.horizontalIndent = 5;
		gd_textLocation.verticalIndent = 5;
		valueText.setLayoutData(gd_textLocation);
		valueText.setText(saturnStartIterationNode.getListkey().trim());
		
		browser = new Label(container, SWT.BORDER);
		browser.setImage(Activator.getImage(Images.AMPLIFY_PLUS)); //$NON-NLS-1$
		final GridData gd_imageLabel = new GridData(SWT.FILL, SWT.CENTER, false, false);
		gd_imageLabel.verticalIndent = 10;
		browser.setLayoutData(gd_imageLabel);

		browser.addMouseTrackListener(new MouseTrackAdapter() {
			public void mouseEnter(final MouseEvent e) {
				browser.setImage(Activator.getImage(Images.AMPLIFY_MINUS)); //$NON-NLS-1$
			}
			public void mouseExit(final MouseEvent e) {
				browser.setImage(Activator.getImage(Images.AMPLIFY_PLUS)); //$NON-NLS-1$
			}
		});
		browser.addMouseListener(new MouseAdapter() {
			public void mouseDown(final MouseEvent e) {
				ElementListSelectionDialog dialog1 = new ElementListSelectionDialog(
						getShell(), new SaturnSWTUtils.SaturnLabelDefaultProvider());
				dialog1.setTitle(Messages.getString("SaturnStartIteratorDialog.ChooseParaValue")); //$NON-NLS-1$
				dialog1.setElements(saturnStartIterationNode.getParameterValueKeys());
				if (dialog1.open() == Window.OK) {
					String ddd = (String) dialog1.getFirstResult();

					valueText.setText(ddd);
				}
			}
		});

		final Label label = new Label(container, SWT.NONE);
		label.setText(Messages.getString("SaturnStartIteratorDialog.LoopListItem")); //$NON-NLS-1$

		itemTxt = new Text(container, SWT.BORDER);
		itemTxt.setTextLimit(100);
		final GridData gd_itemTxt = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
		gd_itemTxt.verticalIndent = 5;
		gd_itemTxt.horizontalIndent = 5;
		itemTxt.setLayoutData(gd_itemTxt);
		itemTxt.setText(saturnStartIterationNode.getItemkey());

		final Label label_1 = new Label(container, SWT.NONE);
		label_1.setText(Messages.getString("SaturnStartIteratorDialog.LoopEndNode")); //$NON-NLS-1$

		endIteratorIdCombo = new Combo(container, SWT.READ_ONLY);
		endIteratorIdCombo.setItems(saturnStartIterationNode.getEndIteratorIds());
		endIteratorIdCombo.setText(saturnStartIterationNode.getEndid());
		final GridData gd_endIteratorIdCombo = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
		gd_endIteratorIdCombo.verticalIndent = 5;
		gd_endIteratorIdCombo.horizontalIndent = 5;
		endIteratorIdCombo.setLayoutData(gd_endIteratorIdCombo);
		
		descriptionLabel = new Label(container, SWT.NONE);
		final GridData gd_descriptionLabel = new GridData(SWT.LEFT, SWT.TOP, false, false);
		gd_descriptionLabel.verticalIndent = 15;
		descriptionLabel.setLayoutData(gd_descriptionLabel);
		descriptionLabel.setText(Messages.getString("SaturnStartIteratorDialog.description"));   //$NON-NLS-1$
		
		descriptionText = new Text(container, SWT.V_SCROLL | SWT.BORDER);
		descriptionText.setTextLimit(200);
		final GridData gd_textDec = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
		gd_textDec.heightHint = 53;
		gd_textDec.horizontalIndent = 5;
		gd_textDec.verticalIndent = 5;
		descriptionText.setLayoutData(gd_textDec);
		descriptionText.setText(saturnStartIterationNode.getDescription());
		
		nameText.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				if("".equals(nameText.getText().trim())){ 
					setErrorMessage(Messages.getString("SaturnStartIteratorDialog.NameLimit"));  //$NON-NLS-1$
					getButton(IDialogConstants.OK_ID).setEnabled(false);
				}else{
					setErrorMessage(null);
					getButton(IDialogConstants.OK_ID).setEnabled(true);
				}
			}
		});
		
		return area;
	}
	
	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID) {
			saturnStartIterationNode.setDescription(descriptionText.getText().trim());
			saturnStartIterationNode.setListkey(valueText.getText().trim());
			saturnStartIterationNode.setItemkey(itemTxt.getText().trim());
			saturnStartIterationNode.setName(nameText.getText().trim());
			saturnStartIterationNode.setEndid(endIteratorIdCombo.getText().trim());
		}
			
		super.buttonPressed(buttonId);
	}
	
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
			createButton(parent, IDialogConstants.OK_ID, Messages
					.getString("ManageConnectionDialog.confirm"), false); //$NON-NLS-1$
			createButton(parent, IDialogConstants.CANCEL_ID, Messages
					.getString("ManageConnectionDialog.cancel"), false); //$NON-NLS-1$
		getButton(IDialogConstants.OK_ID).setEnabled(true);
	}
	
	@Override
	protected Point getInitialSize() {
		return new Point(500, 377);
	}
	
	protected int getShellStyle() {
		return SWT.CLOSE | SWT.RESIZE;
	}
	
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setImage(Activator.getImage(Images.SATURN_ICONS_SMALLNAME)); //$NON-NLS-1$
		newShell.setText(Messages.getString("SaturnStartIteratorDialog.Setparameter")); //$NON-NLS-1$
	}
}